package com.neu.flashmall.user.controller;


import com.neu.common.utils.Result;
import com.neu.flashmall.user.entity.UmsUser;
import com.neu.flashmall.user.service.UmsUserService;
import com.neu.flashmall.user.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/ums_user")
@Api(value = "客户端userController")
public class UmsUserController {
    @Resource
    private UmsUserService umsUserService;

    @GetMapping("/getUserAddresses")
    @ApiOperation(value = "根据userId获取该用户所有地址")
    public Result getUserAddresses(@Param(value = "userId") Long userId){
        return Result.ok().put(umsUserService.getUserAddresses(userId));
    }

    @GetMapping("/usePhoneLogin")
    @ApiOperation(value = "用手机号进行登录")
    public Result usePhoneLogin(@Param(value = "phone") String phone,
                                 @Param(value = "code") String code){
        if(!code.equals("123456")){
            return Result.error().setMessage("验证码错误");
        }
        String result = umsUserService.usePhoneLogin(phone);
        if(result.equals("注册成功")){
            return Result.ok().setMessage(result);
        }
        return Result.error().setMessage(result);
    }

    @GetMapping("/useAccountLogin")
    @ApiOperation(value = "用账号密码进行登录")
    public Result useAccountLogin(@Param(value = "username") String username,
                                @Param(value = "password") String password){
        return Result.ok().put(umsUserService.useAccountLogin(username,password));
    }

    @GetMapping("/getCode")
    @ApiOperation(value = "获取验证码")
    public Result getCode(){
        return Result.ok().put("123456");
    }

    @PostMapping("checkUmsUserName")
    @ApiOperation("查看用户名是否相同")
    public Result checkUmsUserName(@RequestBody UmsUser umsUser){
        return Result.ok().put(umsUserService.checkUmsUserName(umsUser));
    }

    @GetMapping(value = "/getPaged")
    @ApiOperation(value = "获取所有的user信息")
    public Result getAllUmsUser(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "3") int pageSize ){
        return Result.ok().put(umsUserService.getPaged(pageNum,pageSize));
    }

    @GetMapping("/getOneById")
    public Result getOneById(Long id){
        UmsUser result = umsUserService.getOne(id);
        return Result.ok().put(result);
    }

    @PostMapping("/check/insert")
    @ApiOperation("添加用户信息")
    public Result insert(@RequestBody UmsUser umsUser){
        Long lastId = umsUserService.insert(umsUser);
        if(lastId > 0){
            return Result.ok().put(lastId);
        }
        return Result.error();
    }

    @PostMapping("/check/update")
    @ApiOperation("更新商品品牌信息")
    public  Result update(@RequestBody UmsUser umsUser){
        if(umsUserService.update(umsUser)>0){
            return Result.ok().put(true);
        }
        return Result.error();
    }

    @DeleteMapping("/check/deleteUmsUser")
    @ApiOperation("删除用户信息")
    public Result delete( Long id){
        if(umsUserService.delete(id)>0){
            return Result.ok().put(true);
        }
        return Result.error();
    }

    @PostMapping("/login")
    public Result login(String username,String password){
        UserVo user = umsUserService.login(username,password);
        if(user == null){
            return Result.error().setMessage("用户名或者密码错误");
        }
        return Result.ok().put(user);
    }

    @PutMapping("/saveCart")
    public Result saveCart(Long id,String cartJSON){
        String result = umsUserService.saveCart(id, cartJSON);
        if("ok".equals(result)){
            return Result.ok().setMessage(result);
        }
        return Result.error();
    }

    @GetMapping("/getCart")
    public Result getCart(Long id){
        String json = umsUserService.getCart(id);
        return Result.ok().put(json);
    }

    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestBody UmsUser umsUser){
        String password = umsUser.getUsername();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(password);
        umsUser.setPassword(encode);
        return Result.ok().put(umsUserService.update(umsUser));
    }

    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody UmsUser umsUser){
        String password = umsUser.getPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(password);
        umsUser.setPassword(encode);
        return Result.ok().put(umsUserService.update(umsUser));
    }
}
